package com.app.actions.admin;

import com.app.SpringAnnotationDef;
import com.app.actions.BaseAction;
import com.app.model.ORG_Resources;
import com.app.model.User;
import com.app.service.admin.UserService;
import com.app.service.admin.AdminService;
import com.app.utils.PinyinUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-19
 * Time: 上午9:38
 * To change this template use File | Settings | File Templates.
 */
@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Component
@Namespace("/admin") // 这个就是路径 如果写了就是  localhost:9090/admin   然后所有的action都是继承baseaction的
public class SystemCFGAction extends BaseAction {

    //Resource这个是注入spring  这个看代码写模块应该是程序猿应该的基础吧 后面的我就不讲了。
    @Resource(name = SpringAnnotationDef.ADMIN_SERVICE)
    private AdminService service;

    private ORG_Resources resources;


    //下面是注册一个action value的值就是访问路径    localhost:9090/admin/getSystemCfg.action
    //Result 配置的就是返回值。 玩过struts的都知道吧 不说了。
    @Action(value = "getSystemCfg", results = {@Result(name = "menuInfo", location = "/admin/systemcfg/addSystemCfg.jsp"),
            @Result(name = "addresources", location = "/admin/systemcfg/addresources.jsp"),
            @Result(name = SUCCESS, location = "/admin/systemcfg/list.jsp")})
    public String getSystemCfg() {
        //获得所有的资源菜单
        List<ORG_Resources> list = service.getDao().listAll();

        //往request里放attribute
        getRequest().setAttribute("list", list);

        //取得父节点               addresources
        List<ORG_Resources> parentRes = new ArrayList<ORG_Resources>();
        for (ORG_Resources res : list) {
            if (res.getParentId() == 0) {
                parentRes.add(res);
            }
        }
        getRequest().setAttribute("parentRes", parentRes);

        return SUCCESS;
    }

    /**
     * 这个菜单的访问路径就是      localhost:9090/admin/getSystemCfg!addSystemCfg
     * 玩过struts2的都知道。
     * 由于用了dwz 有ajax和json 这个添加的方法没有返回的action 就输出了一个json
     *
     * json是写在BaseAction里的。 BaseAction中也写了很多使用的方法 自己去看
     */
    public void addSystemCfg() {

        String sortName = PinyinUtil.getEname(resources.getResName());
        resources.setSort(sortName);


        service.getDao().saveObject(resources);
        if (resources.getParentId() == 0) {
            //如果新增的是父节点，那么默认的再添加一个同名字的子节点，为了树菜单的好看啊 没办法
            ORG_Resources resources1 = new ORG_Resources();
            resources1.setParentId(resources.getId());
            resources1.setSort(resources.getSort());
            resources1.setResName(resources.getResName());
            resources1.setResUrl("/");

            service.getDao().save(resources1);
        }

        setSort("2");
        JsonOut("添加成功");
    }

    public void delSystemCfg() {
        //得到该资源菜单
        resources = service.getDao().get(resources.getId());
        //删除该菜单
        service.getDao().deleteObject(resources);

        setSort("2");
        JsonOut("删除成功");
    }

    /**
     * 查看该菜单的详细
     *
     * @return
     */
    public String getMenuInfo() {
        resources = service.getDao().get(resources.getId());
        ActionContext context = ActionContext.getContext();
        context.put("resources", resources);
        return "menuInfo";
    }

    public String addresources() {
        List<ORG_Resources> list = service.getDao().getParentRes();
        ActionContext context = ActionContext.getContext();
        context.put("list", list);

        if (resources != null && resources.getId() != null) {
            resources = service.getDao().get(resources.getId());
            context.put("resources", resources);
        }
        return "addresources";
    }


    public AdminService getService() {
        return service;
    }

    public void setService(AdminService service) {
        this.service = service;
    }

    public ORG_Resources getResources() {
        return resources;
    }

    public void setResources(ORG_Resources resources) {
        this.resources = resources;
    }
}
